package algorithms.que13;

import java.util.ArrayList;

/**
 * @功能
 * @创建时间 2020/7/30
 */
public class GeneralizedQueue<Item> {
    public Item[] array;
    /**
     * 最大容量
     */
    public Integer N=10;

    /**
     *当前大小
     */
    public int size;

    public GeneralizedQueue() {
        this.array = (Item[]) new Object[N];
        this.size = 0;
    }

    public boolean isEmpty(){
        return this.size==0;
    }
    public void resize(){

        Item[] tmp = (Item[]) new Object[N];
        for (int i = 0; i < size; i++) {
            tmp[i]=array[i];
        }
        array=tmp;
    }
    public void insert(Item item){
        this.array[size]=item;
        this.size++;
        if(this.size==this.N){
            this.N=2*this.N;
            resize();
        }
    }
    public Item delete(int k){
        Item item = this.array[k-1];
        this.array[k-1]=null; //防止游离
        Item[] tmp = (Item[]) new Object[N];
        for (int i = 0; i < size; i++) {
            if(i<k-1){
                tmp[i]=this.array[i];
            }else if(i>k-1){
                tmp[i-1]=this.array[i];
            }
        }
        this.array=tmp;
        this.size--;
        if(size==this.N/2){
            this.N=size;
            resize();
        }
        return item;
    }
}
